import BaseModel from './baseModel'
import { get } from '../ajax/axios'
import { Api, Admin } from '../ajax/api'
import { ImageMfit } from '../utils/filters'

// 首页轮播图对象
class BannerModel extends BaseModel {

  // 图片链接
  public pic = ''
  // 标题
  public title = ''
  // 详情
  public detil = ''
  // 跳转链接
  public switch = ''
  // 索引
  public index = 0

  /* ----- 获取指定图片大小 -------*/
  // PC端首页图片
  get pcHomeBannerUrl() {
    return ImageMfit(this.pic, 1200, 800)
  }

  static async getModels() {
    const json = await get(Api.banner.all) as any
    return this.modelsWithArr(json)
  }

  // 删除对象
  public removeBanner() {
    return new Promise((resolve, reject) => {
      get(Admin.banner.delete, {
        bannerId: this._id,
      }).then((data) => {
        resolve(data)
      }).catch((err) => {
        reject(err)
      })
    })
  }

  // 添加或者修改banner
  public addOrEddBanner(isEdd: boolean) {
    // const url = isEdd ? Admin.banner.update : Admin.banner.create
    // return new Promise((resolve, reject) => {
    //   post(url, this.getJson(['title', 'detil', 'index', 'switch', 'pic'], {
    //     bannerId: this._id,
    //   })).then(() => {
    //     resolve()
    //   }).catch((err) => {
    //     reject(err)
    //   })
    // })
  }
}

export default BannerModel
